package org.vectrics.recruiter.struts.admin;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Category;
import org.vectrics.SystemException;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.job.JobCandidateStatusReasonType;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

public class SaveJobCandidateStatusReasonAction extends VectricsStrutsAction {
    private static Category log = Category.getInstance(SaveJobCandidateStatusReasonAction.class);
    

	public String doExecute(VectricsActionData actionData)	throws JspException {
		log.debug("doExecute() called");
		String forward = "success";
	    if (actionData.wasButtonPressed("cancel")) {
	        return("cancel");
	    } else {
	        JobCandidateForm form = (JobCandidateForm)actionData.getForm();
	        String jobCandidateId = form.getJobCandidateId();
	        JobCandidate jobCandidate = RecruitServiceLocator.getJobProvider().findJobCandidateById(new Long(jobCandidateId));

	        JobCandidateStatusReasonType reason = RecruitServiceLocator.getJobProvider().findJobCandidateStatusReasonType(form.getStatusReasonCode());
	        if (reason == null)
	        	throw new SystemException("Reason could not be found for code: " + form.getStatusReasonCode());
	        
	        jobCandidate.getCurrentStatus().setJobCandidateStatusReasonType(reason);
        	RecruitServiceLocator.getJobProvider().updateJob(jobCandidate.getJob());
        	ThreadProperties.forcedCommit();
	    }
	    	
		return (forward);
        
    }
    
}
